<template>
  <div class="app-container">
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-input
          v-model="query.keywords"
          clearable
          size="small"
          placeholder="请输入关键词"
          style="width: 200px;"
          class="filter-item"
        />
        <el-input
          v-model="query.bookId"
          clearable
          size="small"
          placeholder="请输入书籍ID"
          style="width: 200px;"
          class="filter-item"
        />
        <el-input
          v-model="query.bookName"
          clearable
          size="small"
          placeholder="请输入书籍名称"
          style="width: 200px;"
          class="filter-item"
        />
        <el-input
          v-model="query.userId"
          clearable
          size="small"
          placeholder="请输入用户ID"
          style="width: 200px;"
          class="filter-item"
        />
        <el-input
          v-model="query.phone"
          clearable
          size="small"
          placeholder="请输入手机号"
          style="width: 200px;"
          class="filter-item"
        />
        <date-range-picker v-model="query.createdAt" class="date-item" />
        <el-select
          v-model="query.projectName"
          clearable
          size="small"
          placeholder="项目"
          class="filter-item"
          style="width: 200px"
        >
          <el-option
            v-for="item in projectOptions"
            :key="item.key"
            :label="item.display_name"
            :value="item.key"
          />
        </el-select>
        <el-select
          v-model="query.taskStatus"
          clearable
          size="small"
          placeholder="状态"
          class="filter-item"
          style="width: 200px"
        >
          <el-option
            v-for="item in dict.task_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation />
      </div>
    </div>
    <div class="tableUp">
      <div class="btnContain">
        <el-button @click="dialog = true">上传任务列表</el-button>
        <el-button @click="crud.doExport">下载任务列表</el-button>
      </div>
    </div>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :height="height" border :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <el-table-column :show-overflow-tooltip="true" prop="keywords" label="关键词" />
      <el-table-column :show-overflow-tooltip="true" prop="projectName" label="项目名称">
        <template slot-scope="scope">
          {{ scope.row.project.projectName }}
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="bookId" label="书籍ID">
        <template slot-scope="scope">
          {{ scope.row.book?scope.row.book.bookId: '' }}
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="bookName" label="书籍名称">
        <template slot-scope="scope">
          {{ scope.row.book?scope.row.book.bookName: '' }}
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="createdAt" label="创建时间" />
      <el-table-column :show-overflow-tooltip="true" prop="expirationTime" label="关键词到期时间" />
      <el-table-column :show-overflow-tooltip="true" label="状态" align="center" prop="taskStatus">
        <template slot-scope="scope">
          <div>
            {{ dict.task_status.filter((item,index) => scope.row.taskStatus === item.value)[0].label }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="150"
        align="center"
      >
        <template v-if="scope.row.taskStatus === 'NEW_TASK'" slot-scope="scope">
          <el-button type="text" size="small" @click="audit(scope.row,true)">过审</el-button>
          <el-button type="text" size="small" @click="audit(scope.row,false)">拒审</el-button>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="projectName" label="回填作品">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="scan(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="userId" label="用户ID">
        <!-- <template slot-scope="scope">
          {{ scope.row.user?scope.row.user.userId: '' }}
        </template> -->
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="phone" label="手机号">
        <template slot-scope="scope">
          {{ scope.row.user?scope.row.user.phone: '' }}
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="邀请人">
        <template slot-scope="scope">
          {{ scope.row.userInvitation?scope.row.userInvitation.invitationCode: '--' }}
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="username" label="用户名">
        <template slot-scope="scope">
          {{ scope.row.user?scope.row.user.username: '' }}
        </template>
      </el-table-column>
    </el-table>
    <pagination />
    <el-dialog :visible.sync="dialog" title="上传文件" :close-on-click-modal="false" append-to-body width="500px" @close="doSubmit">
      <el-upload
        :before-remove="handleBeforeRemove"
        :on-success="handleSuccess"
        :on-error="handleError"
        :limit="1"
        :file-list="fileList"
        :headers="headers"
        :action="baseApi+'/api/task/import'"
      >
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="doSubmit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import crudTask from '@/api/task'
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import { getToken } from '@/utils/auth'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import { mapGetters } from 'vuex'
export default {
  name: 'TaskList',
  components: { rrOperation, pagination, DateRangePicker },
  cruds() {
    return CRUD({ title: '任务', url: 'api/task', crudMethod: { ...crudTask }})
  },
  mixins: [presenter(), header(), crud()],
  // 数据字典
  dicts: ['task_status'],
  data() {
    return {
      height: document.documentElement.clientHeight - 400 + 'px;',
      headers: { 'Authorization': getToken() },
      projectOptions: [
        { key: '0', display_name: 'QQ阅读推文' },
        { key: '1', display_name: '知乎推文' }
      ],
      title: '文件', dialog: false, fileList: [], files: []
    }
  },
  computed: {
    ...mapGetters([
      'baseApi'
    ])
  },
  created() {
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  methods: {
    audit(row, flag) {
      this.$confirm('确定吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await crudTask.auditTask({
          id: row.taskId,
          auditMsg: '',
          isApproved: flag
        })
        this.$message({
          type: 'success',
          message: '已审核!'
        })
        this.crud.toQuery()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    doSubmit() {
      this.fileList = []
      this.dialog = false
      this.crud.toQuery()
    },
    scan(row) {
      this.$router.push({
        path: '/task/goods',
        query: {
          id: row.projectId
        }
      })
    },
    handleBeforeRemove(file, fileList) {
      for (let i = 0; i < this.files.length; i++) {
        if (this.files[i].uid === file.uid) {
          crudTask.del([this.files[i].id]).then(res => {})
          return true
        }
      }
    },
    handleSuccess(response, file, fileList) {
      const uid = file.uid
      const id = response.id
      this.files.push({ uid, id })
    },
    handleError(e, file, fileList) {
      const msg = JSON.parse(e.message)
      this.crud.notify(msg.message, CRUD.NOTIFICATION_TYPE.ERROR)
    }
  }
}
</script>

    <style rel="stylesheet/scss" lang="scss" scoped>
      .tableUp {
        display: flex;
        margin-bottom: 10px;
        justify-content: flex-end;
        .btnContain {
          ::v-deep .el-button {
          background-color: rgba(236, 91, 91, 1);
          color: #fff;
          border: 0;
        }
        }
        .radioContain {
          ::v-deep .el-radio__inner {
            display: none;
          }
          ::v-deep .el-radio__input.is-checked+.el-radio__label {
            color: rgba(236, 91, 91, 1);
          }
        }
      }

    </style>

